package com.danger.Activity.Setting;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.danger.Activity.BackHandledFragment;
import com.danger.Activity.R;
import com.danger.Util.FileCacheService;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by danger on 2016/4/23.
 */
public class AudioRateFragment extends BackHandledFragment implements View.OnClickListener {

    private boolean hadIntercept;
    private View view;
    private RelativeLayout audio_rate_16k_linear;
    private RelativeLayout audio_type_8k_linear;
    private ImageView rate_16k_imageView;
    private ImageView rate_8k_imageView;
    private Map<String, Object> map = new HashMap<String, Object>();
    private FileCacheService service;
    private SettingActivity activity;
    private Toolbar toolbar;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_audio_rate, container, false);
        initView();
        initEvent();
        return view;
    }

    private void initView() {
        activity = (SettingActivity) getActivity();
        service = new FileCacheService(activity);
        audio_rate_16k_linear = (RelativeLayout) view.findViewById(R.id.audio_rate_16k_linear);
        audio_type_8k_linear = (RelativeLayout) view.findViewById(R.id.audio_type_8k_linear);
        rate_16k_imageView = (ImageView) view.findViewById(R.id.rate_16k_imageView);
        rate_8k_imageView = (ImageView) view.findViewById(R.id.rate_8k_imageView);
        toolbar = activity.getToolbar();
        toolbar.setTitle("");
        activity.getTextViewTitle().setText("设置采样率");
        activity.setSupportActionBar(toolbar);
        if (service.getSharePreference("setting").get("audio_rate").equals("16000")) {
            rate_16k_imageView.setVisibility(View.VISIBLE);
            rate_8k_imageView.setVisibility(View.GONE);
        } else {
            rate_16k_imageView.setVisibility(View.GONE);
            rate_8k_imageView.setVisibility(View.VISIBLE);
        }
    }

    private void initEvent() {
        audio_rate_16k_linear.setOnClickListener(this);
        audio_type_8k_linear.setOnClickListener(this);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getFragmentManager().popBackStack();
            }
        });
    }

    @Override
    public boolean onBackPressed() {
        if (hadIntercept) {
            return false;
        } else {
            hadIntercept = true;
            return true;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.audio_rate_16k_linear:
                rate_16k_imageView.setVisibility(View.VISIBLE);
                rate_8k_imageView.setVisibility(View.GONE);
                map.put("audio_rate", "16000");
                break;
            case R.id.audio_type_8k_linear:
                rate_16k_imageView.setVisibility(View.GONE);
                rate_8k_imageView.setVisibility(View.VISIBLE);
                map.put("audio_rate", "8000");
                break;
            default:
                break;
        }
        map.put("audio_subtitle",service.getSharePreference("setting").get("audio_type"));
        map.put("audio_type", service.getSharePreference("setting").get("audio_type"));
        map.put("theme",service.getSharePreference("setting").get("theme"));
        if (service.saveSharePreference("setting", map)) {
            Toast.makeText(activity, "设置成功", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(activity, "设置失败", Toast.LENGTH_SHORT).show();
        }
    }
}